package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.article.pojos.dto.ArticleHomeDto;
import com.usian.model.article.pojos.dto.ArticleInfoDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;


    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Override
    public ResponseResult load(Short type, ArticleHomeDto dto) {

        //1.校验参数
        Integer size = dto.getSize();
        if(size == null || size == 0){
            size = 10;
        }
        //size = Math.min(size,MAX_PAGE_SIZE);
        dto.setSize(size);

        //loadtype  1：加载更多  2：加载最新
        if(!type.equals(1)&&!type.equals(2)){
            type = 1;
        }

        //文章频道校验
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }

        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

        List<ApArticle> articleList = apArticleMapper.loadArticleList(dto, type);

        ResponseResult responseResult = ResponseResult.okResult(articleList);
        responseResult.setHost(fileServerUrl);

        return responseResult;
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {

        Map<String, Object> resultMap = new HashMap<>();

        //1:校验参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2:校验是否下架、删除
        QueryWrapper<ApArticleConfig> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(ApArticleConfig::getArticleId,dto.getArticleId());
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(queryWrapper);

        if(apArticleConfig==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if(!apArticleConfig.getIsDelete() && !apArticleConfig.getIsDown()){

            //3:content 查询  responseResult
            QueryWrapper<ApArticleContent> queryWrapper1 =new QueryWrapper<>();
            queryWrapper1.lambda().eq(ApArticleContent::getArticleId,dto.getArticleId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(queryWrapper1);

            if(apArticleContent ==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }

            resultMap.put("content",apArticleContent);
            resultMap.put("config",apArticleConfig);
        }
        return  ResponseResult.okResult(resultMap);
    }

    @Override
    public List<ApArticle> selectAllArticle() {
        List<ApArticle> apArticlesList = apArticleMapper.selectList(null);
        return apArticlesList;
    }
}
